package cn.wolfcode.web.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.transfer.domain.Appointment;
import cn.wolfcode.transfer.domain.Business;
import cn.wolfcode.transfer.domain.SystemDictionaryItem;
import cn.wolfcode.transfer.qo.AppointmentQueryObject;
import cn.wolfcode.transfer.qo.JsonResult;
import cn.wolfcode.transfer.service.IAppointmentService;
import cn.wolfcode.transfer.qo.QueryObject;
import cn.wolfcode.transfer.service.IBusinessService;
import cn.wolfcode.transfer.service.ISystemDictionaryItemService;
import cn.wolfcode.transfer.util.RequiredPermission;
import cn.wolfcode.transfer.util.StatusUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONReader;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequiredPermission(name = "预约页面", expression = "appointment:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") AppointmentQueryObject qo) {
        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        List<Business> businesses = businessService.listAll();
        List<SystemDictionaryItem> list = systemDictionaryItemService.selectByTypeSN("business");
        model.addAttribute("list",list);
        model.addAttribute("businesses", businesses);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("qo",qo);
        return "appointment/list";
    }


    @RequiredPermission(name = "预约删除", expression = "appointment:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            appointmentService.delete(id);
        }
        return "redirect:/appointment/list";
    }


    @RequiredPermission(name = "预约新增/编辑", expression = "appointment:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Appointment appointment) {
        if (appointment.getId() == null) {
            String ano = DateUtil.format(new Date(), "yyyyMMddHHmmss")+ RandomUtil.randomNumbers(5);
            appointment.setAno(ano);
            DateTime dateTime = new DateTime();
            appointment.setCreateTime(dateTime);
            appointment.setStatus(StatusUtil.履行中.getValue());
            appointmentService.save(appointment);
        } else {
            appointmentService.update(appointment);
        }
        return "redirect:/appointment/list";
    }


    @RequestMapping("/save")
    public String save(Appointment appointment) {
        if (appointment.getId() == null) {
            String ano = DateUtil.format(new Date(), "yyyyMMddHHmmss")+ RandomUtil.randomNumbers(5);
            appointment.setAno(ano);
            DateTime dateTime = new DateTime();
            appointment.setCreateTime(dateTime);
            appointment.setStatus(StatusUtil.预约中.getValue());
            appointmentService.save(appointment);
        } else {
            appointmentService.update(appointment);
        }
        return "redirect:/index";
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long id, int status) {
        appointmentService.updateStatus(id,status);
        return new JsonResult();
    }

    }
